package com.tengju.bff.interfaces.manage.crm.activity.facade;

import com.tengju.bff.interfaces.manage.crm.activity.form.ActivityListForm;
import com.tengju.bff.interfaces.shared.ColumnVO;
import com.tengju.bff.interfaces.shared.DateTimeAssembler;
import com.tengju.bff.interfaces.shared.KVVO;
import com.tengju.bff.interfaces.shared.TableVO;
import com.tengju.motivation.domain.model.activity.Activity;
import com.tengju.motivation.domain.model.activity.ActivityRepository;
import com.tengju.motivation.domain.shared.BasePage;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class ActivityManageFacade {

    private final ActivityRepository activityRepository;

    /**
     * 创建时间在最近90天的活动名称列表
     */
    public List<KVVO<String, Long>> nameList() {
        List<Activity> activityList = activityRepository.getActivityList(null,
                DateTimeAssembler.daysAgo(90));
        return activityList == null ? null : activityList.stream()
//                .filter(Activity::isValid)
                .map(a -> KVVO.of(
                        Optional.ofNullable(a.checkValid())
                                .map(msg -> String.format("【不可用：%s】%s", a.checkValid(), a.getName()))
                                .orElse(a.getName()),
                        a.getActivityId().getValue(),
                        a.getExtraInfo().getActivityType().getVal()))
                .collect(Collectors.toList());
    }

    /**
     * 活动分页列表
     */
    public TableVO list(ActivityListForm form) {

        TableVO table = new TableVO();
        table.addColumn(null, ColumnVO.of("createTime", "创建时间", false));
        table.addColumn(null, ColumnVO.of("endTime", "结束日期", false));
        table.addColumn(null, ColumnVO.of("startTime", "开始日期", false));
        table.addColumn(null, ColumnVO.of("name", "活动名称", false));
        table.addColumn(null, ColumnVO.of("id", "活动ID", false));

        BasePage<Activity> pageData = activityRepository.getActivityList(form.getName(),
                form.getPageIndex(), form.getPageSize());
        List<Map<String, Object>> records = pageData.getDatas() == null ? null : pageData.getDatas().stream()
                .sorted(Comparator.comparing(Activity::getCreateTime).reversed())
                .map(activity -> {
                    Map<String, Object> row = new HashMap<>();
                    row.put("id", activity.getActivityId());
                    row.put("name", activity.getName());
                    row.put("type", activity.getExtraInfo().getActivityType().getVal());
                    row.put("startTime", DateTimeAssembler.formatDateTillSecond(activity.getStartTime()));
                    row.put("endTime", DateTimeAssembler.formatDateTillSecond(activity.getEndTime()));
                    row.put("createTime", DateTimeAssembler.formatDateTillSecond(activity.getCreateTime()));
                    return row;
                }).collect(Collectors.toList());
        table.setRecords(records);
        table.page(pageData.getTotal(), form.getPageIndex(), form.getPageSize());

        return table;
    }

}
